#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:taotieren


"""
    rpi.gpio 需要 root 权限，板上运行需要使用
    sudo python gui_switch.py
"""
from tkinter import *

""" python3 中请安装 tkintertoy 来进行程序调试和验证 """
import RPi.GPIO as GPIO

""" 非树莓派环境下可以用 rpi.gpio_def 代替 rpi.gpio 来进行程序调试和验证 """
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)


class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.check_var = BooleanVar()
        check = Checkbutton(frame, text='Pin 18',
                            command=self.update,
                            variable=self.check_var,
                            onvalue=True,
                            offvalue=False)
        check.grid(row=1)

    def update(self):
        GPIO.output(18, self.check_var.get())


root = Tk()
root.wm_title('On / Off Switch')
app = App(root)
root.geometry("200x50+0+0")
root.mainloop()
